package com.usian.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.pojo.TbContentExample;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ContentCategoryService {
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;
    @Autowired
    private TbContentMapper tbContentMapper;

    public List<TbContentCategory> selectContentCategoryByParentId(Long id) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        return tbContentCategoryMapper.selectByExample(example);
    }

    public Integer insertContentCategory(Long parentId, String name) {
        Date date = new Date();
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setCreated(date);
        tbContentCategory.setUpdated(date);
        tbContentCategory.setParentId(parentId);
        tbContentCategory.setName(name);
        tbContentCategory.setStatus(1);
        return tbContentCategoryMapper.insert(tbContentCategory);
    }

    public PageResult selectTbContentAllByCategoryId(Long categoryId) {
        PageHelper.startPage(1,55555);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = tbContentMapper.selectByExampleWithBLOBs(example);
        PageInfo<TbContent> pageInfo = new PageInfo<>(list);
        return new PageResult(1,pageInfo.getTotal(),pageInfo.getList());
    }

    public Integer updateContentCategory(Long id, String name) {
        Date date = new Date();
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setCreated(date);
        tbContentCategory.setUpdated(date);
        tbContentCategory.setId(id);
        tbContentCategory.setName(name);
        tbContentCategory.setStatus(1);
        return tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
    }

    public Integer deleteContentCategoryById(Long categoryId) {
 /*       TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setId(categoryId);
        tbContentCategory.setStatus(2);
        return tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);*/
        return tbContentCategoryMapper.deleteByPrimaryKey(categoryId);
    }

    public Integer insertTbContent(TbContent tbContent) {
        Date date = new Date();
        tbContent.setUpdated(date);
        tbContent.setCreated(date);
        return tbContentMapper.insert(tbContent);
    }

    public Integer deleteContentByIds(Long ids) {
        return tbContentMapper.deleteByPrimaryKey(ids);
    }
}
